package com.trainfinder.ripper.sweep;

import java.util.Calendar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SweepScheduler {

	private final static Log LOG = LogFactory.getLog(SweepScheduler.class);

	public  void schedule() {

		Sweep sweep = null;
		Calendar c = Calendar.getInstance();

		int hour = c.get(Calendar.HOUR_OF_DAY);

		switch (hour) {
		case 4: // 4 am
			sweep = new GradientSweep();
			break;
		case 5: // 5am
			sweep = new FullSweep();
			break;
		case 15: // 12pm
			sweep = new ShortSweep();
			break;
		case 21: // 5 in the evening
			sweep = new ShortSweep();
			break;
		}

		if (sweep != null) {
			process(sweep, "london", "paris");
			process(sweep, "london", "brussels");
			process(sweep, "london", "lille");
		}

	}

	/**
	 * @param args
	 */
	public  void process(final Sweep instance,
			final String fromStation, final String toStation) {

		Runnable runnable = new Runnable() {

			public void run() {
			
				System.out.println("Starting schedule "
						+ instance.getClass().getName());
				LOG.info("Starting schedule " + instance.getClass().getName());
				instance.sweep(fromStation, toStation);
				LOG.info("Finished schedule " + instance.getClass().getName());
				System.out.println("Finished schedule "
						+ instance.getClass().getName());
			}

		};

		//		
		Thread thread = new Thread(runnable, "thead for "
				+ instance.getClass().getName());

		thread.setDaemon(false);
		LOG.info("Starting thead for " + instance.getClass().getName());
		System.out.println("Starting thead for "
				+ instance.getClass().getName());
		thread.start();
	}

}
